package com.lz.ovuola.repository.mysql;

import javax.sql.DataSource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import com.alibaba.druid.pool.DruidDataSource;
import com.lz.ovuola.util.exception.AppStartupException;

@Configuration
@ConfigurationProperties(prefix = "spring.primary.datasource")
public class PrimaryDataSourceConfig {
	/**
	 * 数据库地址
	 */
	private String url;
	/**
	 * 用户名
	 */
	private String username;
	/**
	 * 密码
	 */
	private String password;
	/**
	 * 驱动
	 */
	private String driverClassName;


	private int maxActive = 30;
	private int minIdle = 5;
	private int initialSize = 20;
	private int maxWait = 1000;

	private boolean removeAbandoned = true;
	private int removeAbandonedTimeout = 180;

	private Logger logger = LoggerFactory.getLogger("PrimaryDataSourceConfig");

	@Bean(name = "primaryDS")
	@Qualifier("primaryDS")
	@Primary
	public DataSource primaryDataSource() {

		if (StringUtils.isNotEmpty(url) && StringUtils.isNotEmpty(username)
				&& StringUtils.isNotEmpty(password)
				&& StringUtils.isNotEmpty(driverClassName)) {
			logger.info(
					"--------primary数据库属性:  url:{},username:{},password:{},driverClassName:{}，maxActive:{}",
					url, username, password, driverClassName, maxActive);
		} else {
			throw new AppStartupException(
					"url,username,password,driverClassName is required");
		}
		DruidDataSource dataSource = new DruidDataSource();
		dataSource.setUsername(username);
		dataSource.setPassword(password);
		dataSource.setUrl(url);
		dataSource.setDriverClassName(driverClassName);
		dataSource.setMaxActive(maxActive);
		dataSource.setMinIdle(minIdle);
		dataSource.setMaxWait(maxWait);
		dataSource.setInitialSize(initialSize);
		dataSource.setRemoveAbandoned(removeAbandoned);
		dataSource.setRemoveAbandonedTimeout(removeAbandonedTimeout);
		return dataSource;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getDriverClassName() {
		return driverClassName;
	}

	public void setDriverClassName(String driverClassName) {
		this.driverClassName = driverClassName;
	}

	public int getMaxActive() {
		return maxActive;
	}

	public void setMaxActive(int maxActive) {
		this.maxActive = maxActive;
	}



	public int getMinIdle() {
		return minIdle;
	}

	public void setMinIdle(int minIdle) {
		this.minIdle = minIdle;
	}

	public int getInitialSize() {
		return initialSize;
	}

	public void setInitialSize(int initialSize) {
		this.initialSize = initialSize;
	}

	public int getMaxWait() {
		return maxWait;
	}

	public void setMaxWait(int maxWait) {
		this.maxWait = maxWait;
	}

	public boolean isRemoveAbandoned() {
		return removeAbandoned;
	}

	public void setRemoveAbandoned(boolean removeAbandoned) {
		this.removeAbandoned = removeAbandoned;
	}

	public int getRemoveAbandonedTimeout() {
		return removeAbandonedTimeout;
	}

	public void setRemoveAbandonedTimeout(int removeAbandonedTimeout) {
		this.removeAbandonedTimeout = removeAbandonedTimeout;
	}

}